package com.orangelife.mobile.parkingcard.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;

public class ParkingCardDetailAdapter<T> extends FatherAdapter<T> {
	
	private Context context;
	private List<Map<String,Object>> list;
	
	public ParkingCardDetailAdapter(Context context,List<Map<String,Object>> list){
		this.context = context;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return ParkingCardDetailAdapter.this;
	}
	
	@SuppressWarnings("unchecked")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_parking_card_detail, null);
			holder.tvPaymentTime = (TextView) convertView.findViewById(R.id.tvPaymentTime);
			holder.tvPaymentMoney = (TextView) convertView.findViewById(R.id.tvPaymentMoney);
			holder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
			holder.tvPaymentLength = (TextView) convertView.findViewById(R.id.tvPaymentLength);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Map<String,Object> map = list.get(position);
		holder.tvPaymentTime.setText(map.get("time").toString());
		holder.tvPaymentMoney.setText("¥"+map.get("amount").toString());
		double coupon_amount = Double.parseDouble(map.get("coupon_amount").toString());
		if(coupon_amount == 0){
			holder.tvDiscount.setVisibility(View.GONE);
		}else{
			holder.tvDiscount.setVisibility(View.VISIBLE);
			holder.tvDiscount.setText("优惠¥"+coupon_amount);
		}
		String quantity = map.get("quantity").toString();
		String unit = map.get("unit").toString();
		holder.tvPaymentLength.setText(quantity+unit);
		return convertView;
	}
	

	class ViewHolder {
		/** 缴费时间 */
		private TextView tvPaymentTime;
		/** 缴费金额*/
		private TextView tvPaymentMoney;
		/** 优惠*/
		private TextView tvDiscount;
		/**缴费时长*/
		private TextView tvPaymentLength;
	}

}
